package com.mdlib.helper.module.account.fragments;

import android.text.Editable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.ljlib.core.widget.CountdownButton;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.common.MDConstant;
import com.mdlib.helper.interfaces.ITextChangedListener;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.model.entity.AgreementEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.utils.MDAppUtils;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by librabin on 16/11/18.
 */

public class RegisterFragment extends BaseTitleFragment {

    @Bind(R.id.et_regist_phone)
    EditText mPhoneNumField;

    @Bind(R.id.btn_featch_code)
    CountdownButton mFeatchCodeButton;

    @Bind(R.id.et_regist_code)
    EditText mEtRegistCode;
    @Bind(R.id.et_regist_pass)
    EditText mEtRegistPass;
    @Bind(R.id.et_regist_pass_again)
    EditText mEtRegistPassAgain;
    @Bind(R.id.cb_regist_tip)
    CheckBox mCbRegistTip;
    @Bind(R.id.tv_regist_protocol)
    TextView mTvRegistProtocol;
    @Bind(R.id.tv_regist_submit)
    TextView mTvRegistSubmit;

    private String mAgreementUrl;

    public static RegisterFragment newInstance() {
        return new RegisterFragment();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_register;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.regist_tv_title, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });


        mPhoneNumField.addTextChangedListener(new ITextChangedListener(){
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 11 && !mFeatchCodeButton.isRunning()) {
                    mFeatchCodeButton.setEnabled(true);
                } else {
                    mFeatchCodeButton.setEnabled(false);
                }
            }
        });

        mCbRegistTip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    mTvRegistSubmit.setEnabled(true);
                } else {
                    mTvRegistSubmit.setEnabled(false);
                }
            }
        });



    }

    @Override
    protected void loadData() {
        super.loadData();
        featchRegAgreement();
    }

    @OnClick({R.id.btn_featch_code, R.id.tv_regist_protocol, R.id.tv_regist_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_featch_code:
                String phoneNum1 = mPhoneNumField.getText().toString();

                if (!MDAppUtils.checkPhoneNum(phoneNum1)) {
                    return;
                }

                featchCode(phoneNum1, "reg");

                break;

            case R.id.tv_regist_protocol:
                UIHelper.showWebPage(getActivity(), mTvRegistProtocol.getText().toString(), mAgreementUrl);
                break;
            case R.id.tv_regist_submit:
                String phoneNum = mPhoneNumField.getText().toString();
                String phoneCode = mEtRegistCode.getText().toString();
                if (!MDAppUtils.checkPhoneNum(phoneNum)) {
                    return;
                }

                if (!MDAppUtils.checkPhoneCode(phoneCode)) {
                    return;
                }

                String password = mEtRegistPass.getText().toString();
                String confirmPwd = mEtRegistPassAgain.getText().toString();

                if (!MDAppUtils.checkPassword(password)) {
                    return;
                }

                if (!password.equals(confirmPwd)) {
                    ToastUtil.showToast("两次输入密码不一致");
                }

                doRegister(phoneNum, password, phoneCode);

                break;
        }
    }



    private void featchCode(String phoneNum, String codeType) {
        addSubscribe(DataManager.getInstance().getAccountApi()
                .featchPhoneCode(phoneNum, codeType)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {

                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));

    }




    private void doRegister(String phoneNum, String password, String phoneCode) {
        addSubscribe(DataManager.getInstance().getAccountApi()
                .register(phoneNum, password, phoneCode, MDConstant.CLIENT_TYPE)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        ToastUtil.showToast(baseResult.msg);
                        removeFragment();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));

    }

    /**
     * 获取用户协议
     */
    private void featchRegAgreement() {
        DataManager.getInstance().getAccountApi()
                .featchRegAgreement()
                .compose(RxHelper.<AgreementEntity>defaultHandleResult())
                .subscribe(new RxSubscribe<AgreementEntity>() {
                    @Override
                    protected void onSucc(AgreementEntity agreementEntity) {
                        mAgreementUrl = agreementEntity.getUrl();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast("获取协议出错");
                    }
                });
    }

    private void getPhoneCode() {
//        Subscription rxSubscription = RetrofitHelper.getInstance().getPTApiService()
//                .doLogin("15062166138", "3", "123456")
//                .compose(RxHelper.<AccountInfo>defaultHandleResult())
//                .subscribe(new RxSubscribe<AccountInfo>() {
//                    @Override
//                    protected void onSucc(AccountInfo accountInfo) {
//                    }
//
//                    @Override
//                    protected void onFail(String msg) {
//                    }
//                });
//
//        addSubscribe(rxSubscription);

    }

}
